/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package jfinance;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.IOException;
import java.util.LinkedList;

/**
 *
 * @author efim
 */
public class Account {
    
    public static final String NEW_TRANSACTION_EVENT = "Account.NEW_TRANSACTION";
    
    public static final String SET_TRANSACTION_EVENT = "Account.SET_TRANSACTION";
    
    public static final String DELETE_TRANSACTION_EVENT = "Account.DELETE_TRANSACTION";
    
    public static final String UPDATE_AMOUNT_EVENT = "Account.UPDATE_AMOUNT";
    
    public static final String UPDATE_NAME_EVENT = "Account.UPDATE_NAME";
    
    private String name;
    
    private LinkedList<Transaction> transactions;
    
    //private double amount;
    
    {
        transactions = new LinkedList<>();
        //amount = 0.0;
    }
    
    public Account(String name){
        this.name = name;
    }
    
    public Account(String name,LinkedList<Transaction> transactions){
        this.name = name;
        this.transactions = transactions;
    }
    
    public String getName(){
        return name;
    }
    
    public double getAmount(){
        double result = 0;
        for(Transaction g: transactions){
            result = g.calculateAmount(result);            
        }
        return result;
    }
    
    public LinkedList<Transaction> getTransactions(){
        return transactions;
    }
    
    public void addTransaction(Transaction transaction){
        
        transactions.add(transaction);
        firePropertyChange(Account.NEW_TRANSACTION_EVENT, null, name);
    }
    
    public void deleteTransaction(int id) {
        Transaction t = transactions.get(id);
        transactions.remove(id);
        firePropertyChange(Account.DELETE_TRANSACTION_EVENT, t, null);
    }
    
    public void deleteTransaction(Transaction transaction) {        
        transactions.remove(transaction);
        firePropertyChange(Account.DELETE_TRANSACTION_EVENT, transaction, null);
    }
    
    
    public void setTransactionById(int id,Transaction transaction){
        Transaction old = transactions.get(id);
        transactions.set(id, transaction);
        firePropertyChange(Account.SET_TRANSACTION_EVENT, old, transaction);
    }
    public Transaction getTransactionById(int id){
        return transactions.get(id);
    }
    public void editName(String name){
        String old = this.name;
        this.name = name;
        firePropertyChange(Account.UPDATE_NAME_EVENT, old, name);
        //Model.saveChanges();
    }
    
    
    private final PropertyChangeSupport propertyChangeSupport
            = new PropertyChangeSupport(this);

    public void addPropertyChangeListener(
            PropertyChangeListener listener) {
        propertyChangeSupport.
                addPropertyChangeListener(listener);
    }

    private void firePropertyChange(
            String propertyName, Object oldValue,
            Object newValue) {
        propertyChangeSupport.firePropertyChange(
                propertyName, oldValue, newValue);
    }

    
}
